using Digitalmes.Application.Mes.Equipments.Alarms;
using Digitalmes.Application.Mes.Equipments.Alarms.Commands;
using Digitalmes.Application.Mes.Equipments.Alarms.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Equipments;

/// <summary>
/// 设备警报定义项终结点。
/// </summary>
public sealed class AlarmItemEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/equipment", "alarmItem")
            .RequireAuthorization()
            .MapGet(GetAlarmItemPagedListAsync, "list-page")
            .MapPost(CreateAlarmItemAsync)
            .MapPut(UpdateAlarmItemAsync, "{id}")
            .MapDelete(DeleteAlarmItemAsync, "{id}");
    }

    [EndpointDescription("分页查询设备警报定义项")]
    public async Task<IApiResult> GetAlarmItemPagedListAsync(ISender sender, [AsParameters] GetAlarmItemListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建设备警报定义项")]
    public async Task<IApiResult> CreateAlarmItemAsync(ISender sender, EquipAlarmItemDto dto)
    {
        var result = await sender.Send(new CreateAlarmItemCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新设备警报定义项")]
    public async Task<IApiResult> UpdateAlarmItemAsync(ISender sender, long id, EquipAlarmItemDto dto)
    {
        var result = await sender.Send(new UpdateAlarmItemCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除设备警报定义项")]
    public async Task<IApiResult> DeleteAlarmItemAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteAlarmItemCommand(id));
        return result.ToApiResult();
    }
}
